import logType from '../common/const/logType';
import { Up, getDevice } from '../common/device';
import dayjs from 'dayjs';
import { UAParser } from 'ua-parser-js';

var nowRegex = [[/(Now)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]];
var parser = new UAParser(
    { browser: nowRegex }
);


export interface Log {
    uin: number | string,
    userAgent: string,
    date: number,
    all: string,
    msg: string,
    ip: string,
    level: logType,
    from: string,
    version: string,
    rowNum?: number,
    colNum?: number,
    index: number,
    time: number
}

export interface speed {
    aegis_id: number,
    url: string,
    create_time: string,
    avg_time: number,
    times: number
}

export interface dailyStatic {
    title: string,
    total: number
}

export enum speedType {
    STATIC = 'static',
    FETCH = 'fetch'
}

interface region {
    id: number,
    country: string,
    region: string,
    province: string,
    city: string,
    isp: string
}
export interface FormatLog extends Log {
    platform: Icon[],
    appIcon: Icon[],
    webview: string[],
    time: number,
    device: any,
    version: string,
    index: number,
    msg: string,
    region?: region,
    _index: number
}

export interface Icon {
    name: string,
    version: string | null | boolean
}
let indexFlag = 0
export function formatLog(log: Log): FormatLog {
    const device = getDevice(log.userAgent);
    parser.setUA(log.userAgent);
    const result = parser.getResult();
    const formatLog = Object.assign(log, {
        time: dayjs(log.time || log.date).format('YYYY-MM-DD HH:mm:ss'),
        uin: log.uin || '-',
        device,
        appIcon: [result.browser],
        platform: [result.os],
        webview: [`${result.engine.name}:${result.engine.version}`],
        version: log.version || 1,
        index: log.index,
        _index: indexFlag
    });
    indexFlag += 1;

    return formatLog;
}
